<template>
  <!-- 对话框表单 -->
  <el-dialog class="ba-operate-dialog"
             :close-on-click-modal="false"
             :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
             @close="baTable.toggleForm"
             :destroy-on-close="true">
    <template #header>
      <div class="title"
           v-drag="['.ba-operate-dialog', '.el-dialog__header']"
           v-zoom="'.ba-operate-dialog'">
        {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
      </div>
    </template>
    <el-scrollbar v-loading="baTable.form.loading"
                  class="ba-table-form-scrollbar">
      <div class="ba-operate-form"
           :class="'ba-' + baTable.form.operate + '-form'"
           :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
        <el-form ref="formRef"
                 @keyup.enter="baTable.onSubmit(formRef)"
                 :model="baTable.form.items"
                 :label-position="config.layout.shrink ? 'top' : 'right'"
                 :label-width="baTable.form.labelWidth + 'px'"
                 v-if="!baTable.form.loading">
          <el-form-item label="产品ID"
                        prop="product_id">
            <el-input v-model="baTable.form.items!.product_id"
                      disabled>
            </el-input>
          </el-form-item>
          <el-form-item label="1件"
                        prop="num_1">
            <el-input v-model="baTable.form.items!.num_1">
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="2件"
                        prop="num_2">
            <el-input v-model="baTable.form.items!.num_2">
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="3件"
                        prop="num_3">
            <el-input v-model="baTable.form.items!.num_3">
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="4件"
                        prop="num_4">
            <el-input v-model="baTable.form.items!.num_4">
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="5+件"
                        prop="num_above_5">
            <el-input v-model="baTable.form.items!.num_above_5">
              <template #append>%</template>
            </el-input>
          </el-form-item>
          <el-form-item label="退款比例"
                        prop="refund_rate">
            <el-input v-model="baTable.form.items!.refund_rate">
              <template #append>%</template>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
        <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
        <el-button v-blur
                   :loading="baTable.form.submitLoading"
                   @click="baTable.onSubmit(formRef)"
                   type="primary">
          {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import { useAdminInfo } from '/@/stores/adminInfo'
import { useConfig } from '/@/stores/config'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

watch(
    () => baTable.form.operate,
    (newVal) => {
        //rules.mobile![0].required = newVal == 'Add'
    }
)
</script>